package com.test.application.reflex_mechanism;

import lombok.Data;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 描述: TODO
 * 作者: yanruizhi
 * 时间: 2023/9/12 15:57
 */
@Data
public class DogProxy {
    private Dog dog;

    public  DogProxy(Dog dog) {
        this.dog = dog;
    }

    public Object getProxyInstance() {
        //1.获取目标对象的类加载器
        ClassLoader classLoader = dog.getClass().getClassLoader();
        //2.获取目标对象的实现接口类型
        Class<?>[] interfaces = dog.getClass().getInterfaces();
        //3.获取InnvocationHandler处理器
        InvocationHandler invocationHandler = new InvocationHandler(){
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("前置处理");
                Object invoke = method.invoke(dog, args);
                System.out.println("后置处理");
                return null;
            }
        };

        return Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
    }
}
